//
//  HDMenuView.m
//  JYG_Sales
//
//  Created by Mike on 15/10/26.
//  Copyright (c) 2015年 WeShare. All rights reserved.
//

#define padding 0

#import "HDMenuView.h"

@interface HDMenuView()
@property(nonatomic,strong)UIView *selectedView;
@end
@implementation HDMenuView

- (id)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = titleArray;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    _selectedView = [[UIView alloc] initWithFrame:CGRectMake(padding, self.height-2, (self.width - padding * 2 * _titleArray.count) / _titleArray.count, 2)];
    _selectedView.backgroundColor = kBlueColor;
    [self loadBtns];
}

- (void)setFont:(UIFont *)font
{
    _font = font;
    for (int i=0; i<_titleArray.count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:100+i];
        btn.titleLabel.font = font;
    }
}

- (void)setNormalColor:(UIColor *)normalColor
{
    _normalColor = normalColor;
    for (int i = 0 ; i <_titleArray.count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:100+i];
        [btn setTitleColor:normalColor forState:UIControlStateNormal];
    }
}

- (void)setSelectedColor:(UIColor *)selectedColor
{
    _selectedColor = selectedColor;
    for (int i = 0 ; i <_titleArray.count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:100+i];
        [btn setTitleColor:selectedColor forState:UIControlStateSelected];
    }
}

- (void)setBottomLineColor:(UIColor *)bottomLineColor
{
    _bottomLineColor = bottomLineColor;
    _selectedView.backgroundColor = bottomLineColor;
}

- (void)setSelectedViewHeight:(float)selectedViewHeight{
    _selectedViewHeight = selectedViewHeight;
    _selectedView.top = self.height-selectedViewHeight;
    _selectedView.height = selectedViewHeight;
}

//是否隐藏灰色分割线
- (void)setIsHiddenGrayLine:(BOOL)isHiddenGrayLine
{
    _isHiddenGrayLine = isHiddenGrayLine;
    if (self.isHiddenGrayLine == NO) {
        UIImageView *menuLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height-0.5, self.width, 0.5)];
        menuLine.image = [UIImage imageNamed:@"dot"];
        [self  addSubview:menuLine];
    }
}

- (void)loadBtns
{
    for (int i=0; i<_titleArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*self.width/_titleArray.count, 0, self.width/_titleArray.count, self.height);
        if (self.font == nil)
        {
            btn.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
        }
        if (self.normalColor == nil)
        {
            [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        }
        if (self.selectedColor == nil)
        {
            [btn setTitleColor:kBlueColor forState:UIControlStateSelected];
        }
        [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100+i;
        [self addSubview:btn];
        if (i == 0) {
            btn.selected = YES;
            [self moveSelectedView:0];
        }
    }
}

- (void)setTitleWithTitle:(NSString*)title AtIndex:(NSInteger)index{
    UIButton *btn = (UIButton *)[self viewWithTag:100+index];
    [btn setTitle:title forState:UIControlStateNormal];
}

- (void)setSelectedIndex:(int)value
{
    _selectedIndex = value;
    for (int i = 0 ; i <_titleArray.count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:100+i];
        btn.selected = i==value;
        if (btn.selected && self.selectedFont) {
            btn.titleLabel.font = self.selectedFont;
        }else {
            btn.titleLabel.font = self.font;
        }
    }
    [self moveSelectedView:_selectedIndex];
    if ([self.delegate respondsToSelector:@selector(selectMenuIndex:)]) {
        [self.delegate selectMenuIndex:_selectedIndex];
    }
}

- (void)clickBtn:(UIButton *)sender
{
    self.selectedIndex = (int)sender.tag-100;
    for (int i=0; i<_titleArray.count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:100+i];
        if (btn.tag!=sender.tag) {
            btn.selected = NO;
        }
    }
}

- (void)moveSelectedView:(int)selectedIndex
{
    if (!self.selectedView.superview) {
        [self addSubview:self.selectedView];
    }
    [UIView animateWithDuration:0.2 animations:^{
        
        self.selectedView.left = padding +(self.width / _titleArray.count) * selectedIndex;
    }]; 
}

@end
